﻿using Controller;
using Model.Course;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Model.User;

public partial class ViewCoursesLogged : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Teacher.get(Convert.ToInt32(Session["Person"])) != null)
            addCourseToolbarButton.Visible = true;

        Course[] courses = CourseController.viewCourses();

        Control  control = (Control)Master.Master.FindControl("contentPlaceHolder");

        foreach (var course in courses)
        {
            LinkButton link = new LinkButton();
            link.CommandName = "CourseId";
            link.CommandArgument = course.getId().ToString();
            link.Text = course.getName();
            link.Font.Size = FontUnit.Parse("1.5em");
            link.Click += link_Click;

            control.Controls.Add(link);
            control.Controls.Add(new HtmlGenericControl("br"));
        }
    }

    void link_Click(object sender, EventArgs e)
    {
        LinkButton link = (LinkButton)sender;
        Session["CourseId"] = link.CommandArgument;

        Teacher t = Teacher.get(Convert.ToInt32(Session["Person"]));
        Student s = Student.get(Convert.ToInt32(Session["Person"]));

        if ((t != null && t.isLeadingCourse(
                        Convert.ToInt32(link.CommandArgument)))
            ||
            (s != null && s.isSignedToCourse(
                        Convert.ToInt32(link.CommandArgument))))
        {
            Session["ActiveMenuButton"] = "menuButton9";
            Response.Redirect("~/CourseView/ViewCourseLogged.aspx");
        }
        else
        {
            Response.Redirect("~/CourseView/ViewCourseDescriptionLogged.aspx");
        }
    }

    protected void addCourseToolbarButton_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(((ImageButton)sender).CommandArgument);
    }
}